package com.demo.common.utils;

import com.alibaba.fastjson2.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

// 工具类
public class ResponseUtils {
    public static void writeJson(HttpServletResponse response,Object date)  {
        // 设置响应头
        response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = null;
        // 获取写对象
        try {
            writer = response.getWriter();
            // 写数据
            writer.write(JSONObject.toJSONString(date));
            writer.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            writer.close();
        }
    }
}
